package de.baumann.browser.activitys.center

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.Menu
import android.view.MenuItem
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.activitys.center.CreatePoolActivity.Companion.startCreatePoolActivity
import de.baumann.browser.activitys.center.LicenceGiftRecordActivity.Companion.startLicenceGiftRecordActivity
import de.baumann.browser.activitys.center.RequestLicenceActivity.Companion.startRequestLicenceActivity
import de.baumann.browser.activitys.hash.ImproveHashActivity
import de.baumann.browser.adapter.LicenceListAdapter
import de.baumann.browser.api.net.vo.Licence
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.ILicenceManagerView
import de.baumann.browser.present.LicenceManagerPresenter
import de.baumann.browser.views.dialog.LicenceGiftDialog
import de.baumann.browser.views.dialog.RuleDialog
import kotlinx.android.synthetic.main.activity_licence_manager.*
import kotlinx.android.synthetic.main.include_tool_bar.*

/**
 *@author:
 *@date:2018/12/19 16:06
 *@discription: licence 管理
 */
class LicenceManagerActivity : BaseOdinActivity<ILicenceManagerView, LicenceManagerPresenter>(), ILicenceManagerView {

    private lateinit var licenceListAdapter: LicenceListAdapter

    companion object {
        fun startLicenceManagerActivity(context: Context) {
            context.startActivity(Intent(context, LicenceManagerActivity::class.java))
        }
    }

    override fun getTitleText(): String {
        return getString(R.string.licence_management)
    }

    override fun getLayout(): Int {
        return R.layout.activity_licence_manager
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnGift.setOnClickListener {
            LicenceGiftDialog(mContext, object : LicenceGiftDialog.OnGiftNowLisntener {
                override fun giftNow(account: String) {
                    presenter.licenceGift(account)
                }
            }).show()
        }

        tvProxy.setOnClickListener {
            startRequestLicenceActivity(mContext, licenceListAdapter.itemCount)
        }

        tvImproveHash.setOnClickListener {
            ImproveHashActivity.startImproveHashActivity(mContext)
        }

        tvCreatePool.setOnClickListener {
            startCreatePoolActivity(mContext)
        }

        tvGiftRule.setOnClickListener {
            RuleDialog(mContext)
                    .setRuleTitle("规则说明")
                    .setRuleContent(getString(R.string.licenceManagerRule))
                    .show()
        }

        rvLicenceList.layoutManager = LinearLayoutManager(mContext)
        licenceListAdapter = LicenceListAdapter(R.layout.item_licence_list)
        rvLicenceList.adapter = licenceListAdapter
    }

    override fun createPresenter(): LicenceManagerPresenter {
        return LicenceManagerPresenter()
    }

    override fun createView(): ILicenceManagerView {
        return this
    }

    override fun initData() {
        presenter.getInActivatedLicences()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_transaction, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        startLicenceGiftRecordActivity(mContext)
        return super.onOptionsItemSelected(item)
    }

    override fun setLicences(licences: List<Licence>) {
        if (licences.isNullOrEmpty()) {
            return
        }
        btnGift.visibility = View.VISIBLE
        licenceListAdapter.setNewData(licences)
    }

    override fun canCreatePool() {
        startCreatePoolActivity(mContext)
    }

    override fun giftSuccess() {
        showShortToast("转赠成功")
        presenter.getInActivatedLicences()
    }

    override fun getLicenceId(): String {
        return licenceListAdapter.licenceId
    }
}
